<template>
  <div class="components-container">
    <!-- 弹窗  -->
    <sweet-modal ref="dialog" class="dialog">
      <h3>{{title}}</h3>
      <div style="margin:30px 50px">
        <div class="layui-form-item">
            <div class="layui-inline" v-if="false">
              <label class="layui-form-label">类型</label>
              <div class="layui-input-inline">
                <el-select v-model="selectData.CYPTYPE" filterable :disabled="isshow">
                  <el-option v-for="item in sexType" :key="item.CodeValue" :label="item.CodeName" :value="item.CodeValue">
                  </el-option>
                </el-select>
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">类别名称</label>
              <div class="layui-input-inline">
                <el-input type="text" v-model="selectData.CYPNAME" :disabled="isshow"></el-input>
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">序号</label>
              <div class="layui-input-inline">
                <el-input type="text" v-model="selectData.CYPNUM" :disabled="isshow"></el-input>
              </div>
            </div>
            <div class="layui-inline" v-if="isshowstatus">
              <label class="layui-form-label">状态</label>
              <div class="layui-input-inline">
                <el-select v-model="selectData.CYPSTATUS" placeholder="请选择状态" :disabled="isshow">
                    <el-option  key="A" label="正常" value="A"></el-option>
                    <el-option  key="B" label="禁用" value="B"></el-option>
                </el-select>
              </div>
            </div>
        </div>
      </div>
      <div class="layui-form-item btnCenter">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" :loading="this.$store.state.isloading" @click="submit">提交</el-button>
      </div>
    </sweet-modal>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
</style>
<script>
import sweetModal from "@/components/sweetModal";
import apiBase from "@/api/apiBase.js";
const ADD = "新增";
const MODIFY = "修改";
const DELETE = "删除";
const INFO = "详情";
export default {
  name: "CremationfurnaceMain",
  data() {
    return {
      title: null,
      creTypes: [],
      title: null,
      order: null,
      creNo: null,
      creType: null,
      creStatus: null,
      selectData:{
          CYPTYPE: null,
          CYPNAME: null,
          CYPNUM: null,
          CYPSTATUS: 'A'
      },
      isshow: false,
      isshowstatus: true,
      CYPID: null,
    };
  },
  methods: {
    //功能操作
    open(order, currentRow) {
      this.order = order;
      this.dataRepeat();
      this.isshowstatus = true;
      this.isshow = false;
      if(currentRow){
          this.CYPID = currentRow.CYPID;
      };
      switch (this.order) {
        case ADD:
          this.title = "收费大类新增";
          this.isshowstatus = false;
          this.$refs.dialog.open();
          break;
        case MODIFY:
          this.title = "收费大类修改";
          this.dataInfo(currentRow.CYPID);
        //   this.initSelectData();
          this.$refs.dialog.open();
          break;
        case INFO:
          this.title = "收费大类详情";
          this.dataInfo(currentRow.CYPID);
          this.isshow = true;
          this.$refs.dialog.open();
          break;
        case DELETE:
          this.dataDelete(currentRow.CYPID);
          break;
        default:
      }
    },
    //提交
    submit() {
      this.$store.state.isloading = true;
      switch (this.order) {
        case ADD:
          this.dataAdd();
          break;
        case MODIFY:
          this.dataModify();
          break;
        case DELETE:
          break;
        case INFO:

          break;
        default:
      }
    },
    //获取类型
    getCreTypes() {
      apiBase.DoService(
        {},
        apiBase.getApiAdress().Code.GetCremationType,
        res => {
          let creTypeData = res.data.content,
            legendsCreData = creTypeData.map(item => ({
              label: item.CodeName,
              value: item.CodeValue
            }));
          this.creTypes = legendsCreData;
        }
      );
    },
    //新增
    dataAdd() {
        let data = JSON.parse(JSON.stringify(this.selectData));
        apiBase.DoService(data,apiBase.getApiAdress().Cyp.Create,
            res => {
                this.$refs.dialog.close();
                this.refreshData();
                this.$message({
                    message: "收费大类新增成功！",
                    type: "success"
                });
            },
            res => {
                this.$message.error(res.data.msg);
            }
        );
    },
    //详情
    dataInfo(id) {
      var data = {
        CYPID: id
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Cyp.Detail,
        res => {
          this.selectData = res.data.content;
        }
      );
    },
    //修改
    dataModify() {
      let data = JSON.parse(JSON.stringify(this.selectData));
      data.CYPID = this.CYPID;
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Cyp.Modify,
        res => {
          this.$refs.dialog.close();
          this.refreshData();
          this.$message({
            message: "修改成功！",
            type: "success"
          });
        },
        res => {
          this.$message.error(res.data.msg);
        }
      );
    },
    //删除
    dataDelete(id) {
      var data = {
        CYPID: id,
      };
      this.$confirm("确认删除该数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          apiBase.DoService(
            data,
            apiBase.getApiAdress().Cyp.Delete,
            res => {
              this.refreshData();
              this.$message({
                message: "删除成功！",
                type: "success"
              });
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //数据重置
    dataRepeat() {
      Object.assign(this.$data.selectData,this.$options.data().selectData);
    },
    //初始化下拉框数据
    initSelectData() {
      this.getCreTypes();
    },
    refreshData() {
      this.$emit("refreshData");
    },
    closeDialog() {
      this.$refs.dialog.close();
    }
  },
  components: {
    sweetModal
  },
  mounted() {},
  created() {}
};
</script>